package com.jinxiangyang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Teacher;
import com.jinxiangyang.domain.vo.TeacherQueryVo;
import com.jinxiangyang.service.TeacherService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/teacher")
@Api(tags = "教师接口")
@Slf4j
@CrossOrigin
public class TeacherController {

    @Autowired
    TeacherService teacherService;


    /**
     * 查询所有老师
     *
     * @return
     */

    @GetMapping
    @ApiOperation(value = "查询所有老师")
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
    public AjaxResult findAllTeacher() {
        return AjaxResult.success(teacherService.list());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询老师")
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
    public AjaxResult findTeacherById(@PathVariable String id) {
        return AjaxResult.success(teacherService.getById(id));
    }

    /**
     * 添加老师
     * @param teacher
     * @return
     */

    @PostMapping
    @ApiOperation(value = "添加老师")
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.INSERT)
    public AjaxResult addTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.save(teacher);
        if (flag) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */


    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除老师")
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.DELETE)
    public AjaxResult delTeacher(@PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    @PutMapping
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.UPDATE)
    public AjaxResult updTeacher(@RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @PostMapping("/{currentPage}/{pageSize}")
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
    public AjaxResult findTeacherByPage(@PathVariable long pageSize, @PathVariable long currentPage, @RequestBody TeacherQueryVo teacherQueryVo) {
        if (teacherQueryVo == null) {
            return teacherService.findTeacherByPage(currentPage, pageSize);
        } else {
            return teacherService.findTeacherByQuery(currentPage, pageSize, teacherQueryVo);
        }
    }

    @DeleteMapping
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.DELETE)
    public AjaxResult delTeacherByIds(@RequestBody String[] ids) {
        boolean flag = teacherService.removeBatchByIds(Arrays.asList(ids));
        if (flag) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 上传头像
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @OperateLog(title = "讲师模块", operatorType = OperatorType.ADMIN, businessType = BusinessType.DELETE)
    public AjaxResult avatarUpload(MultipartFile file) {
        String url = teacherService.avatarUpload(file);
        return AjaxResult.success("上传成功", url);
    }

}
